package com.fouram.mybatis.base;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.ToString;

import java.io.Serializable;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

@ApiModel(description = "分页结果")
@ToString
public final class PageInfo<T> implements Serializable {
	private static final long serialVersionUID = 1L;

	@ApiModelProperty("数据总条数")
	private Long total;

	@ApiModelProperty("分页结果数据列表")
	private List<T> data;

	public static <T> PageInfo<T> empty(Class<T> clazz) {
		List<T> emptyList = Collections.emptyList();
		return new PageInfo(0L, emptyList);
	}

	public static <T> PageInfo<T> empty() {
		return new PageInfo(0L, Collections.emptyList());
	}

	public boolean isEmpty() {
		return Objects.equals(0L, this.total) || this.data == null || this.data.isEmpty();
	}

	@Override
	public boolean equals(Object o) {
		if (this == o) {
			return true;
		} else if (o != null && this.getClass() == o.getClass()) {
			PageInfo<?> pageInfo = (PageInfo)o;
			return Objects.equals(this.total, pageInfo.total) && Objects.equals(this.data, pageInfo.data);
		} else {
			return false;
		}
	}

	@Override
	public int hashCode() {
		return Objects.hash(new Object[]{this.total, this.data});
	}

	public Long getTotal() {
		return this.total;
	}

	public List<T> getData() {
		return this.data;
	}

	public void setTotal(Long total) {
		this.total = total;
	}

	public void setData(List<T> data) {
		this.data = data;
	}

	public PageInfo() {
	}

	public PageInfo(Long total, List<T> data) {
		this.total = total;
		this.data = data;
	}
}